home *** CD-ROM | disk | FTP | other *** search
- * Program..: NAMEVARS.PRG
- * Author...: Steve Titterud, a modified version of another author's work.
- * Notes....: This utility saves a little, but only a little, of the drudge
- * work in writing dBase programs. It will generate blocks of code
- * to be used within programs and write them to a filename the
- * user specifies, for later block copying into the appropriate
- * part of your program.
- * Generates memvar names from field names, writes initialization
- * text for memvars to blanks, for memvars to fields, and replace
- * statements, all for later copying into actual program segment.
- * Must pass datafile name (default extension is .dbf) and text
- * output filename (no extension defaults to .txt) to this program.
- * If no directory given, current directory is default.
- * Also can create a datafile, "m_%datafile", with all fieldnames
- * equal to datafile fieldnames, prefixed by "m_". (for UI.exe).
- set echo off
- SET TALK OFF
- set safety off
- notquit=.T.
- do while notquit
- clear
- * establish datafile name and m_datafile name; if datafile not found, quit...
- dirdata=space(15)
- dirtext=space(15)
- datafile=space(12)
- textfile=space(12)
- @ 0,0 say "We are about to write code fragments based on fields of datafile to a textfile."
- @ 2,5 say "Specify directory and filename for both datafile and textfile..."
- @ 3,5 say "(defaults if no value specified: current directory, .dbf, and .txt)"
- @ 7,5 say "Directory of datafile? " get dirdata
- @ 7,45 say "Datafile? " get datafile
- @ 9,5 say "Directory of textfile? " get dirtext
- @ 9,45 say "Textfile? " get textfile
- @ 22,20 say "<Esc> to quit."
- read
- r=readkey()
- if r=12 .or. r=268
- set talk on
- set echo on
- set safety on
- return
- endif
- ** The four if-endif blocks of active code accessed through 4 switches:
- mem2blanks=.F.
- mem2flds=.F.
- replflds=.F.
- doUI=.F.
- @ 14,5 say " Of the four outputs: 1. Initialize memvars to blanks."
- @ 15,5 say " 2. Initialize memvars to field values."
- @ 16,5 say " 3. Generate replacement statements."
- @ 17,5 say " 4. Create m_"+datafile+" for use with UI."
- choices=" "
- do while len(trim(choices))=0 .or. .not. ("1"$choices .or. "2"$choices .or. "3"$choices .or. "4"$choices)
- choices=" "
- @ 19,5 say "Which do you want ?" get choices
- @ 19,30 say "(no delimiters needed: e.g., 134 or 24, etc.)"
- read
- enddo
- r=readkey()
- if r=12 .or. r=268
- set talk on
- set echo on
- set safety on
- return
- endif
- ** set switches to values based upon content of choices
- mem2blanks=iif("1"$choices,.T.,.F.)
- mem2flds=iif("2"$choices,.T.,.F.)
- replflds=iif("3"$choices,.T.,.F.)
- doUI=iif("4"$choices,.T.,.F.)
- if (mem2blanks .or. mem2flds .or. replflds .or. doUI) && at least 1 output selected
- ** establish all filenames
- dirdata=ltrim(rtrim(dirdata))
- dirtext=ltrim(rtrim(dirtext))
- datafile=ltrim(rtrim(datafile))
- textfile=ltrim(rtrim(textfile))
- dirdata=iif(len(dirdata)=0,"",iif(right(dirdata,1)="\",dirdata,dirdata+"\"))
- dirtext=iif(len(dirtext)=0,"",iif(right(dirtext,1)="\",dirtext,dirtext+"\"))
- datafile=iif("."$datafile,datafile,datafile+".dbf")
- textfile=iif("."$textfile,textfile,textfile+".txt")
- m_datafile="m_"+substr(datafile,1,at(".",datafile)-1)
- m_datafile=substr(m_datafile,1,8)
- m_datafile=m_datafile+substr(datafile,at(".",datafile),4)
- datafile=dirdata+datafile
- textfile=dirtext+textfile
- m_datafile=dirtext+m_datafile
- IF FILE(datafile)
- USE &datafile
- clear
- @ 10,20 say "Creating files..."
- set console off
- ELSE && it's not here - go back and do again
- clear
- @ 10,10 say datafile + " not found. Re-check file name or location of file."
- @ 23,0
- wait
- loop
- ENDIF
- * Copy twice to stru exte files to set up field names in primary and secondary...
- copy to noprefix stru exte
- COPY TO prefix STRUCTURE EXTENDED
- select 2
- use noprefix
- select 1
- USE prefix
- ** match up fields with relation on record number
- set relation to recno() into noprefix
- ** Change field names (which become memvars) to lower case...
- ** prefix will hold fields with "m_" prefix
- REPLACE ALL Field_name WITH "m_"+LOWER(Field_name)
- CLEAR
- * Establish alternate file for text output...
- SET ALTERNATE TO &textfile
- SET ALTERNATE ON
- set console on
- clear
- if mem2blanks
- @ 10,10 say "Writing initializations as blanks to "+textfile+"...."
- set console off
- * Initialize memvars to blank values
- STORE "00000000000000000000" TO zeros
- go top
- DO WHILE .NOT. EOF()
- DO CASE
- CASE Field_type = "C"
- ? Field_name+" = SPACE("+STR(Field_len,3)+")"
- CASE Field_type = "N" .AND. Field_dec = 0
- ? Field_name +" = " + SUBSTR(zeros,1,Field_len-Field_dec)
- CASE Field_type = "N" .AND. Field_dec > 0
- ? Field_name +" = "+SUBSTR(zeros,1,Field_len-Field_dec-1)+"."+SUBSTR(zeros,1,Field_dec)
- CASE Field_type = "L"
- ? Field_name+" = .F."
- CASE Field_type = "D"
- ? Field_name+[ = CTOD(" / / ")]
- ENDCASE
- SKIP
- ENDDO
- endif
- if mem2flds
- * Initialize memvars to field values...
- set console on
- clear
- @ 10,10 say "Writing initializations as field values to "+textfile+"...."
- set console off
- GO TOP
- ?
- DO WHILE .NOT. EOF()
- ? Field_name+" = "+noprefix->Field_name
- SKIP
- ENDDO
- endif
- if replflds
- * Write REPLACE statements...
- set console on
- clear
- @ 10,10 say "Writing REPLACE statements to "+textfile+"...."
- set console off
- GO TOP
- ?
- DO WHILE .NOT. EOF()
- ? "REPLACE " + noprefix->Field_name + " WITH "+Field_name
- SKIP
- ENDDO
- ? && one last carriage return
- endif
- if doUI
- ** generate datafile to use with Wallsoft's UI.exe since they made
- ** no provision in early version to hand over memvars named from fields;
- ** might as well delete this whole part if they fix it in later version..
- set console on
- clear
- @ 10,10 say "Creating "+m_datafile+" from "+datafile+"...."
- create &m_datafile from prefix
- endif
- erase prefix.dbf && no more need for these...
- erase noprefix.dbf
- clear
- @ 10,5 say textfile+" now holds initialization and REPLACE statements."
- @ 12,5 say iif(doUI,m_datafile+" holds the renamed fields for UI...","")
- @ 23,30 say "Do another? (Y/N)" get notquit
-
- read
- enddo
- endif && if at least one output selected
- * Close up...
- set ALTERNATE TO
- CLOSE DATABASES
- set console on
- SET TALK ON
- set echo on
- set safety on
- clear
- RETURN